/*
 *说明：用户凭证-申请静态口令脚本
 *
 *@author liuxun 2012-03-06
*/

var opeForm;
var operationType;

$(function(){

	opeForm=$('#opeForm');

	if ( $('#release_form').val() != '02') {
		$('#static_pwd_md5TR').hide();	// 隐藏静态密码
	}
	
	changeIsLongTerm();
	
	$('#okBtn').click(doOk);
	$('#cancelBtn').click(closeWin);
	
});

// 发放形式值变事件
function changeReleaseForm() {
	
	var release_form = $('#release_form').val();
	
	if (isEmpty(release_form)) {
		$('#static_pwd_md5').val('');
		$('#static_pwd_md5TR').hide();
	}else if(release_form == '02') {
		$('#static_pwd_md5TR').show();
	}else {
		$('#static_pwd_md5').val('');
		$('#static_pwd_md5TR').hide();
	}
}

//口令明文和密文切换
function static_pwd_md5Change(){
	var temp = document.getElementById('static_pwd_md5').value;
	if($("#showPass").attr("checked")){
		document.getElementById('pass').innerHTML='<input type="text" id="static_pwd_md5" name="usr.static_pwd_md5" required="true" validType="" class="easyui-validatebox  editMainText" value="${model.password}" maxlength="50">&nbsp;&nbsp;';
	}else{
		document.getElementById('pass').innerHTML='<input type=password id="static_pwd_md5" name="usr.static_pwd_md5" required="true" validType="" class="easyui-validatebox  editMainText" value="${model.password}" maxlength="50">&nbsp;&nbsp;';
	} 
	document.getElementById('static_pwd_md5').value=temp;
}


function changeIsLongTerm() {
	var is_long_term = $('#is_long_term').val();
	if (is_long_term == '0') {
		$('#effect_timeTR').show();
		$('#expire_timeTR').show();
	} else if (is_long_term == '1'){
		$('#effect_time').attr('required', '');
		$('#effect_timeTR').attr('required', '');
		$('#effect_time').val('');
		$('#expire_time').val('');
		$('#effect_timeTR').hide();
		$('#expire_timeTR').hide();
	}
}

//保存按钮操作
function doOk() {
	var validateResult = opeForm.form('validate');

	if (!validateResult){
		showInfoWin('请正确填写信息!');
		return;
	}

	var release_form = $('#release_form').val();
	if (isEmpty(release_form)) {
		showInfoWin('发放形式不能为空!');
		return;
	}else{

	}
	
	var is_long_term = $('#is_long_term').val();
	if (is_long_term == '0') {
		var effect_time = $('#effect_time').val();
		if (isEmpty(effect_time)) {
			showInfoWin('生效时间不能为空!');
			return;
		}
		
		var expire_time = $('#expire_time').val();
		if (isEmpty(expire_time)) {
			showInfoWin('失效时间不能为空!');
			return;
		}
		
		if (expire_time < effect_time) {
			showInfoWin('失效时间不能小于生效时间!');
			return;
		}
	}
	
	var options = '';
	if(release_form == '01'){  // 密码信封
		options = { 
			url: 'B_USR_CREDENCE!doApplyStaticPwd.do',
			type: 'post',
			success: function(data){
				printOperation(data.usr_id, data.static_pwd_md5);
			}
		};
	}else{ //管理员设置
		options = { 
			url: 'B_USR_CREDENCE!doApplyStaticPwdAdminSetup.do',
			type: 'post',
			success: function(data){
				showInfoWin('操作成功!', function() {window.close();});
			}
		};
	}
	opeForm.ajaxSubmit(options);
}

//打印
function printOperation(usr_id, static_pwd_md5) {
	$('#static_pwd_md5').val(static_pwd_md5);
	opeForm.attr('action', 'B_USR_CREDENCE!doStaticPwdPrint.do?usr.key='+ usr_id);
	opeForm.attr('target', 'hideWindow');
	opeForm.submit();
}

function winClose(){
	window.close();
}
